<template>
  <div class="addProtocol">
    <el-dialog
      title="更换点表"
      :visible.sync="replaceProtocolDialogShow"
      class="box"
      :before-close="handleClose"
    >
      <el-form :model="replaceForm">
        <el-form-item label="当前点表：" :label-width="formLabelWidth">
          <input v-model="replaceForm.table_name" disabled class="typeInput disableInput"/>
        </el-form-item>
        <el-form-item label="替换点表：" :label-width="formLabelWidth">
        <template  slot-scope="scope">
        <div class="grid-content bg-purple">
        <el-upload
        class="upload-demo"
        action=""
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :http-request="uploadProtocol"
        multiple
        :limit="1"
        :on-exceed="handleExceed">
        <el-button style="margin-bottom: 10px; color: #52C2D2;" size="small">点击上传</el-button>
        <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
        </el-upload>
        </div>
        </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :disabled="confirmbtn" type="primary" @click="confirmReplace()">保 存</el-button>
        <el-button type="info" @click="handleClose()">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'

  export default {
    props: ['userIndex', 'userRow'],
    data() {
      return {
        replaceForm: {
          table_name: '',
          attris_file: ''
        },
        dialogBack: false,
        confirmbtn: true,
        formLabelWidth: '85px',
        // 图片上传
        fileList: [],
        // 以下为接口
        replaceProtocol: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'projects_type_replace',
          data: {
            resource_id: '',
            attris_file: ''
          }
        }
      }
    },
    created() {
      this.replaceProtocolDialogShow = true
    },
    mounted() {
      this.formParent()
      // this.queryData()
    },
    methods: {
      // 图片上传
      handleRemove(file, fileList) {
        console.log(file, fileList)
        if (fileList.length === 0) {
          this.confirmbtn = true
        }
      },
      handlePreview(file) {
        console.log(file)
      },
      handleExceed(files, fileList) {
        // this.$message.warning(`上传失败，当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
        this.$message.warning(`上传失败，当前限制选择 1 个文件。`)
      },
      uploadProtocol(item) {
        const formData = new FormData()
        formData.append('file', item.file)
        console.log(item)
        // this.protocolUpload.file = item.file
        // formData.append('group', 'system')
        axios.post('/protocol/upload', formData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
          .then((res) => {
            this.replaceForm.attris_file = res.data.data.file_name
            console.log(res)
            this.confirmbtn = false
          })
          .catch((error) => {
            this.$message({
              type: 'error',
              message: '请上传 .spl 格式文件'
            })
            this.confirmbtn = true
            console.log(error)
          })
      },
      // 子组件传值
      handleClose() {
        const dialogShow = this.dialogBack // 所需要传的值
        this.$emit('giveFalse', dialogShow) // 传值的参数
      },
      // 接受父组件的值
      formParent() {
        console.log(this.userRow)
        console.log(this.userIndex)
        this.replaceForm.table_name = this.userRow.table_name
      },
      // 确认修改
      confirmReplace() {
        this.replaceProtocol.data.resource_id = this.userRow.resource_id
        this.replaceProtocol.data.attris_file = this.replaceForm.attris_file
        axios.post('/protocol', this.replaceProtocol, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          console.log(res)
        })
          .catch(function(error) {
            console.log(error)
          })
        this.handleClose()
      }
      // 以下为获取数据方法
      // 获取列表信息
      //      queryData() {
      //        axios.post('/heartbeat', this.submitdata, {
      //          headers: {
      //            'Content-Type': 'application/x-www-form-urlencoded'
      //          }
      //        }).then((res) => {
      //          console.log(res)
      //        })
      //          .catch(function(error) {
      //            console.log(error)
      //          })
      //      }
    }
  }
</script>

<style scoped>
  /*****************第一层弹窗*****************/
  /deep/ .box > .el-dialog{
    width: 540px !important;
    min-height: 200px;
    margin-top: 30vh !important;
    border-radius: 4px;
  }
  /*****************输入框*****************/
  .typeInput{
    width: 425px;
    height: 25px;
    border-radius: 4px;
    border: 1px solid #ccc;
    outline: none;
    text-indent: 10px;
  }
  .disableInput{
    border: none;
    background-color: #fff;
  }
  input::-webkit-input-placeholder{
    color: #C0C4CC;
  }
  /*****************第一层弹窗头部*****************/
  /deep/ .box > .el-dialog > .el-dialog__header{
    width: 540px;
    height: 39px;
    line-height: 43px;
    padding: 0;
    text-indent: 20px;
    font-weight: bolder;
    border-radius: 4px 4px 0 0;
    background-color: #e3e6ee;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn{
    top: 12px;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn > i{
    font-size: 18px;
    font-weight: bolder;
  }
  /*****************第一层弹窗内容*****************/
  /deep/ .box > .el-dialog > .el-dialog__body{
    padding: 0 0 0 10px ;
    margin-top: 15px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item{
    margin-bottom: 0px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__label{
    color: #69727E;
    padding: 0;
    font-weight: normal;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__content > .el-input > .el-input__inner{
    width: 240px;
    height: 30px;
    padding: 0 5px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .borderNone > .el-form-item__content > .el-input > .el-input__inner{
    border: none;
  }
  /*****************第一层弹窗底部*****************/
  /deep/ .box > .el-dialog > .el-dialog__footer{
    border-top: 1px solid #e3e6ee;
    padding: 15px;
  }
  /*****************底部按钮*****************/
  /deep/ .dialog-footer{
    padding-top: 8px;
  }
  /deep/ .dialog-footer > .el-button{
    width: 80px;
    height: 30px;
    padding: 0;
  }
</style>
